package com.itwlj.controller.admin;

import com.itwlj.dto.SubCategoryDTO;
import com.itwlj.dto.SubCategoryPageQueryDTO;
import com.itwlj.entity.SubCategory;
import com.itwlj.result.PageResult;
import com.itwlj.result.Result;
import com.itwlj.service.SubCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 子分类管理
 */
@RestController
@RequestMapping("admin/subcategory")
@Api(tags = "子分类相关接口")
@Slf4j
public class SubCategoryController {

    @Autowired
    private SubCategoryService subCategoryService;

    /**
     * 新增子分类
     *
     * @param subCategoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增子分类")
    public Result insert(@RequestBody SubCategoryDTO subCategoryDTO) {
        log.info("新增子分类：{}", subCategoryDTO);
        subCategoryService.insert(subCategoryDTO);
        return Result.success();
    }

    /**
     * 子分类分页查询
     *
     * @param subCategoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("子分类分页查询")
    public Result<PageResult> page(SubCategoryPageQueryDTO subCategoryPageQueryDTO) {
        log.info("子分类分页查询：{}", subCategoryPageQueryDTO);
        PageResult pageResult = subCategoryService.pageQuery(subCategoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除子分类
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除子分类")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除子分类：{}", ids);
        subCategoryService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改子分类
     *
     * @param subCategoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改子分类")
    public Result<String> update(@RequestBody SubCategoryDTO subCategoryDTO) {
        log.info("修改子分类：{}", subCategoryDTO);
        subCategoryService.update(subCategoryDTO);
        return Result.success();
    }

    /**
     * 查询子分类
     *
     * @return
     */
    @GetMapping
    @ApiOperation("查询子分类")
    public Result<List<SubCategory>> list() {
        log.info("查询子分类");
        List<SubCategory>  subCategoryList = subCategoryService.list();
        return Result.success(subCategoryList);
    }
}
